package com.chat.service.impl;

import com.chat.constant.MessageConstant;
import com.chat.constant.PasswordConstant;
import com.chat.constant.StatusConstant;
import com.chat.dto.EmployeeCheckDTO;
import com.chat.dto.EmployeeDTO;
import com.chat.dto.EmployeeLoginDTO;
import com.chat.entity.Employee;
import com.chat.exception.AccountLockedException;
import com.chat.exception.AccountNotFoundException;
import com.chat.exception.PasswordErrorException;
import com.chat.mapper.EmployeeMapper;
import com.chat.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
public class EmployeeServiceImpl  implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    // 员工登录
    public Employee login(EmployeeLoginDTO employeeLoginDTO){
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        Employee employee =  employeeMapper.getByUsername(username);


        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // 对前端用户输入的密码进行MD5加密 并与数据库加密密码比对
        /*password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }*/

        //普通密码比对
        if(!password.equals(employee.getPassword())){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

//        if (employee.getStatus() == StatusConstant.DISABLE) {
//            //账号被锁定
//            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
//        }

        //3、返回实体对象
        return employee;


    }

    //新增员工
    @Override
    public void addEmployee(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        employee.setStatus(StatusConstant.ENABLE);//ENABLE=1 DISABLE=0
//        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        /*//设置创始人与修改人ID*/

        /*employee.setCreateUser(BaseContext.getCurrentId());*/
        /*employee.setUpdateUser(BaseContext.getCurrentId());*/

        //将这所有的数据存储到数据库中
        employeeMapper.insertEmployee(employee);
    }

    //注册校验
    @Override
    public String registerCheck(EmployeeCheckDTO employeeCheckDTO) {
        String stuNumber = employeeCheckDTO.getStuNumber();
        String username = employeeCheckDTO.getUsername();
        String phone = employeeCheckDTO.getPhone();
        List<Employee> employees = new ArrayList<>();
        employees = employeeMapper.getRegisterCheck(employeeCheckDTO);
        System.out.println(employees);
        if (employees.isEmpty()) {
            return null;
        } else {
            for (Employee employee : employees) {
                if (employee.getStuNumber().equals(stuNumber)) {
                    return "学号已存在!";
                } else if (employee.getUsername().equals(username)) {
                    return "用户名已存在!";
                } else if (employee.getPhone().equals(phone)) {
                    return "手机号已存在!";
                }
            }
        }
        return null;
    }
}
//for (Employee employee : employees) {
//        if (employee.getStuNumber() == stuNumber) {
//        return "学号已存在!";
//        }else if (employee.getUsername() == username) {
//        return "用户名已存在!";
//        }else if (employee.getPhone() == phone) {
//        return "手机号已存在!";
//        }
//        }